<?php

namespace iBrand\Component\Purchase\Repositories\Eloquent;

use iBrand\Component\Purchase\Models\TakeStockItem;
use iBrand\Component\Purchase\Repositories\TakeStockItemRepository;
use Prettus\Repository\Eloquent\BaseRepository;

class TakeStockItemRepositoryEloquent extends BaseRepository implements TakeStockItemRepository
{
    public function model()
    {
        return TakeStockItem::class;
    }

    public function getTakeStockItemsPaginate($where, $limit = 20)
    {
        return $this->scopeQuery(function ($query) use ($where) {
            $query = $query->where(function ($query) use ($where) {
                if (count($where)) {
                    foreach ($where as $key => $value) {
                        if ($key == 'take_stock_id') {
                            $query = $query->whereIn($key, $value);
                        } else {
                            if (is_array($value)) {
                                list($operate, $va) = $value;
                                $query = $query->where($key, $operate, $va);
                            } else {
                                $query = $query->where($key, $value);
                            }
                        }
                    }
                }
            });
            return $query->orderBy('created_at', 'desc');
        })->paginate($limit);
    }

    public function getTakeStockItemsPaginateByPlanId($plan_id, $limit = 20)
    {
        return $this->scopeQuery(function ($query) use ($plan_id) {
            return $query->whereHas('takeStock', function ($query) use ($plan_id) {
                return $query->where('plan_id', $plan_id);
            });
        })->paginate($limit);
    }

    public function getTakeStockItemsExcel($where, $limit = 20)
    {
        $data = $this->scopeQuery(function ($query) use ($where) {
            $query = $query->where(function ($query) use ($where) {
                if (count($where)) {
                    foreach ($where as $key => $value) {
                        if ($key == 'take_stock_id') {
                            $query = $query->whereIn($key, $value);
                        } else {
                            if (is_array($value)) {
                                list($operate, $va) = $value;
                                $query = $query->where($key, $operate, $va);
                            } else {
                                $query = $query->where($key, $value);
                            }
                        }
                    }
                }
            });
            return $query->orderBy('created_at', 'desc');
        })->paginate($limit);

        //'商品名称', '商品编号', 'SKU','尺码', '颜色', '吊牌价', '盘点数量'

        $lastPage = $data->lastPage();
        foreach ($data as $key=>$item) {
            $list[$key]['name'] = $item->product->goods->name;
            $list[$key]['goods_no'] = $item->product->goods->goods_no;
            $list[$key]['sku'] = $item->product->sku;
            $list[$key]['color'] = $item->meta['color'];
            $list[$key]['size'] = $item->meta['size'];
            $list[$key]['market_price'] = $item->product->goods->market_price;
            $list[$key]['num'] = $item->count;
        }

        return [
            'products' => $list,
            'lastPage' => $lastPage,
        ];

    }
}